package org.edu.modules.portal.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.portal.entity.VeSiteCategory;
import org.edu.modules.portal.service.IVeSiteCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;


@Api(tags = {"栏目信息"})
@RestController
@RequestMapping({"/portal/veSiteCategory"})
@ApiSort(30)
public class VeSiteCategoryController
        extends BaseController<VeSiteCategory, IVeSiteCategoryService> {
    private static final Logger log = LoggerFactory.getLogger(org.edu.modules.portal.controller.VeSiteCategoryController.class);


    @Autowired
    private IVeSiteCategoryService veSiteCategoryService;


    @AutoLog("栏目信息-分页查询")
    @ApiOperation(value = "分页查询", notes = "栏目信息-分页查询")
    @PostMapping({"/list"})
    @ApiOperationSupport(order = 1)
    public Result<?> queryPageList(VeSiteCategory veSiteCategory, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "99") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<VeSiteCategory> queryWrapper = QueryGenerator.initQueryWrapper(veSiteCategory, req.getParameterMap());
        Page<VeSiteCategory> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeSiteCategory> pageList = this.veSiteCategoryService.page((IPage) page, (Wrapper) queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog("栏目信息-查询栏目树")
    @ApiOperation(value = "查询栏目树", notes = "栏目信息-查询栏目树")
    @PostMapping({"/queryTree"})
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "siteid", value = "网站id", required = true), @ApiImplicitParam(name = "caterowmodel", value = "关联模型代码: 师资建设=szjs，校企合作=xqhz", required = false), @ApiImplicitParam(name = "pid", value = "上级栏目ID", required = false), @ApiImplicitParam(name = "type", value = "类型: 空=全部模块，Node=默认模块", required = false)})
    public Result<?> queryTree(@ApiIgnore VeSiteCategory veSiteCategory, HttpServletRequest req) {
        QueryWrapper<VeSiteCategory> queryWrapper = QueryGenerator.initQueryWrapper(veSiteCategory, req.getParameterMap());
        JSONArray jArray = this.veSiteCategoryService.queryTree((Wrapper) queryWrapper, veSiteCategory.getPid());
        return Result.OK(jArray);
    }


    @AutoLog("栏目信息-通过id查询")
    @ApiOperation(value = "通过id查询", notes = "栏目信息-通过id查询")
    @PostMapping({"/queryById"})
    @ApiOperationSupport(order = 3)
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeSiteCategory veSiteCategory = (VeSiteCategory) this.veSiteCategoryService.getById(id);
        if (veSiteCategory == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veSiteCategory);
    }


    @AutoLog("栏目信息-新增信息")
    @ApiOperation(value = "新增信息", notes = "栏目信息-新增信息")
    @PostMapping({"/add"})
    @ApiOperationSupport(order = 4)
    public Result<?> add(@RequestBody VeSiteCategory veSiteCategory) {
        if (this.veSiteCategoryService.save(veSiteCategory)) {
            return Result.OK("添加成功！");
        }

        return Result.error("添加失败！");
    }


    @AutoLog("栏目信息-修改信息")
    @ApiOperation(value = "修改信息", notes = "栏目信息-修改信息")
    @PostMapping({"/edit"})
    @ApiOperationSupport(order = 5)
    public Result<?> edit(@RequestBody VeSiteCategory veSiteCategory) {
        if (this.veSiteCategoryService.updateById(veSiteCategory)) {
            return Result.OK("编辑成功！");
        }

        return Result.error("编辑失败！");
    }


    @AutoLog("栏目信息-通过id删除")
    @ApiOperation(value = "通过id删除", notes = "栏目信息-通过id删除")
    @PostMapping({"/delete"})
    @ApiOperationSupport(order = 6)
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        if (this.veSiteCategoryService.removeById(id)) {
            return Result.OK("删除成功!");
        }

        return Result.error("删除失败！");
    }


    @AutoLog("栏目信息-批量删除")
    @ApiOperation(value = "批量删除", notes = "栏目信息-批量删除")
    @PostMapping({"/deleteBatch"})
    @ApiOperationSupport(order = 7)
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        if (this.veSiteCategoryService.removeByIds(Arrays.asList(ids.split(",")))) {
            return Result.OK("批量删除成功!");
        }

        return Result.OK("批量删除失败!");
    }
}


